package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* NFLView
* */
class NFLView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_0C46E6)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_DC1A1A)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 13).coerceAtLeast(minStrokeWidth)
            mPaint0.strokeWidth = mStrokeWidth
            mPaint1.strokeWidth = mStrokeWidth

            drawCircle(
                0F,
                0F,
                cx * 11 / 12,
                mPaint0
            )

            drawArc(
                -cx * 2,
                0F,
                0F,
                cy * 2,
                275F,
                80F,
                false,
                mPaint0
            )

            drawArc(
                0F,
                -cy * 2,
                cx * 2,
                0F,
                95F,
                80F,
                false,
                mPaint0
            )

            drawLine(
                cx * 11 / 12 * Math.cos(Math.toRadians(45.0)).toFloat() - mStrokeWidth / 2,
                cx * 11 / 12 * Math.sin(Math.toRadians(45.0)).toFloat() - mStrokeWidth / 2,
                cx * 11 / 12 * Math.cos(Math.toRadians(225.0)).toFloat() + mStrokeWidth / 2,
                cx * 11 / 12 * Math.sin(Math.toRadians(225.0)).toFloat() + mStrokeWidth / 2,
                mPaint1
            )

            drawLine(
                cx * 11 / 12 * Math.cos(Math.toRadians(135.0)).toFloat() + mStrokeWidth / 2,
                cx * 11 / 12 * Math.sin(Math.toRadians(135.0)).toFloat() - mStrokeWidth / 2,
                cx * 11 / 12 * Math.cos(Math.toRadians(315.0)).toFloat() - mStrokeWidth / 2,
                cx * 11 / 12 * Math.sin(Math.toRadians(315.0)).toFloat() + mStrokeWidth / 2,
                mPaint0
            )

            restore()
        }
    }
}